
import http, { baseParams } from "@/utils/request";
import { UrlGetBillList, UrlAddBill, UrlGetBillSumInfo } from "@/utils/apiUrl";
import { isEmptyString } from "@/utils/utils";
import { Toast } from "antd-mobile";


/**
 * 获取账单汇总信息
 * @returns thunk
 */
export const getBillSumInfo = (reqCallback) => {
    return async (dispatch) => {
        console.log("getBillSumInfo: " + UrlGetBillSumInfo);

        //组装参数
        const params = baseParams();

        //发起请求
        const res = await http.post(UrlGetBillSumInfo, params)
        const resCode = parseInt(res.code)
        const resObj = res.data
        console.log("获取账单汇总信息-结束：=======" + resCode)
        console.log(resObj)
        console.log("获取账单汇总信息-结束：=======")

        //保存
        const success = (resCode === 0)

        //如果有错误，提示
        if (!success) {
            if (!isEmptyString(res.msg)) {
                Toast.show(res.msg, 1)
            }
        }

        //回调
        reqCallback && reqCallback(success, resObj)
    }
}

/**
 * 获取账单
 * @returns thunk
 */
export const getBills = (reqCallback) => {
    return async (dispatch) => {
        console.log("getBills: " + UrlGetBillList);

        //组装参数
        const params = baseParams();

        //发起请求
        const res = await http.post(UrlGetBillList, params)
        const resCode = parseInt(res.code)
        const resObj = res.data
        console.log("获取账单-结束：=======" + resCode)
        console.log(resObj)
        console.log("获取账单-结束：=======")

        //保存
        const success = (resCode === 0)

        //如果有错误，提示
        if (!success) {
            if (!isEmptyString(res.msg)) {
                Toast.show(res.msg, 1)
            }
        }

        //回调
        reqCallback && reqCallback(success, resObj)
    }
}


/**
 * 新增账单
 * @returns thunk
 */
export const addBill = (type, amount, tag, detail, reqCallback) => {
    return async (dispatch) => {
        console.log("addBill: " + UrlAddBill);

        //组装参数
        const params = baseParams();
        params.type = type;
        params.amount = amount;
        params.tag = tag;
        params.detail = detail;

        //发起请求
        const res = await http.post(UrlAddBill, params)
        const resCode = parseInt(res.code)
        const resObj = res.data
        console.log("新增账单-结束：=======" + resCode)
        console.log(resObj)
        console.log("新增账单-结束：=======")

        //保存
        const success = (resCode === 0)

        //如果有错误，提示
        if (!success) {
            if (!isEmptyString(res.msg)) {
                Toast.show(res.msg, 1)
            }
        }

        //回调
        reqCallback && reqCallback(success, resObj)
    }
}
